package ims.servlet;

import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.update.prodType.UpdateProdTypeDAO;
import ims.db.update.prodType.UpdateProdTypeDbRequest;
import ims.db.update.prodType.UpdateProdTypeDbResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ViewProdTypeServlet
 */
@WebServlet("/ViewProdTypeServlet")
public class UpdateProdTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateProdTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String newProdType = request.getParameter("prodType");
			String prodTypeId = request.getParameter("prodTypeId");
			
			response.setContentType("text/html;charset=UTF-8");
	
			//Retrieve all product types
			UpdateProdTypeDbRequest dbRequest = new UpdateProdTypeDbRequest();
			ProductType prodType = new ProductType();
			prodType.setProdTypeId(prodTypeId);
			prodType.setProdType(newProdType);
			dbRequest.setProdType(prodType);
			UpdateProdTypeDAO db = new UpdateProdTypeDAO();
			UpdateProdTypeDbResponse dbResponse = (UpdateProdTypeDbResponse) db.execute(dbRequest);
			
			String nextLoc = "/IMS/prodType?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
